var elderBeHospitalArray = null;
var elderBeHospital = {};

var elderBeHospitalArrayForSelect = null;
var selectedElderBeHospital = null;


var getSelectedElderBeHospital = function() {
	return elderBeHospital;
}

$(function() {
	$('#vue_vm_list div').hide();
	$('#ulOutDate').hide();
	$('#btnOutOrderOK').hide();
	
	$('#elderFloor_select').html('<option value="" selected></option>');
	$('#elderBeHospital_select').html('<option value="" selected></option>');
	$('#elder_select').html('<option value="" selected></option>');
	
	loadFloorSelectBySubjectRoomId(function() {
		_addChangeListener("#elderFloor_select", function() {
			var floorId = $('#elderFloor_select').val();
			loadBeHospitalSelectByFloorId(floorId);
			loadElderSelectByFloorId(floorId);
		});
	});
});

function loadFloorSelectBySubjectRoomId(onSuccess) {
	if($('#elderFloor_select').length==0) {return;}

	$('#elderFloor_select').html('<option value="" selected></option>');
	$('#elderBeHospital_select').html('<option value="" selected></option>');
	
	var loginedUser = getLoginedUser();

	find_basic_by_json(path+'/floor/findBySubjectRoomId', {subjectRoomId: loginedUser.subjectRoomId}, function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#elderFloor_select').append('<option value="' + response.data[i].floorId
				+ '">' + response.data[i].buildingName + ' ' + response.data[i].floorName + '</option>');
		}
		
		if(onSuccess) {
			onSuccess(response);
		}
	});
}

function loadBeHospitalSelectByFloorId(floorId) {
	if(!floorId || floorId=='' || $('#elderBeHospital_select').length==0) {return;}

	$('#elderBeHospital_select').html('<option value="" selected></option>');

	find_basic_by_json(path+'/elderBeHospital/findByFloorId', {floorId: floorId}, function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#elderBeHospital_select').append('<option value="' + response.data[i].beHospitalId
				+ '">' + response.data[i].bedNumber +'床'+' '+ response.data[i].roomNumber+' '+response.data[i].elderForm.name
				+ ' '+(response.data[i].houseTypeType==1?"VIP":"普通") +'</option>');
		}
		
		elderBeHospitalArray = response.data;
		
		_addChangeListener("#elderBeHospital_select", function() {
			var option = $('#elderBeHospital_select option:selected');
			var beHospitalId = $('#elderBeHospital_select').val();
			
			for(var i=0; i<elderBeHospitalArray.length; i++) {
				if(elderBeHospitalArray[i].beHospitalId==beHospitalId) {
					elderBeHospital = elderBeHospitalArray[i];
				}
			}
			
			if(view && view.data) {
				view.data = elderBeHospital;
				
				view.data.elderId = elderBeHospital.elderForm.elderId;
				view.data.elderName = elderBeHospital.elderForm.name;
			}
			
			if(elderFamilyMemberView && elderFamilyMemberView.data) {
				elderFamilyMemberView.data.elderId = elderBeHospital.elderForm.elderId;
				elderFamilyMemberView.data.elderName = elderBeHospital.elderForm.name;
			}
			
			if(load_elderFamilyMember_by_elderId) {
				load_elderFamilyMember_by_elderId(elderBeHospital.elderForm.elderId);
			}
		});
	});
}

function loadElderSelectByFloorId(floorId) {
	if(!floorId || floorId=='' || $('#elder_select').length==0) {return;}

	$('#elder_select').html('<option value="" selected></option>');

	find_basic_by_json(path+'/elderBeHospital/findByFloorId', {floorId: floorId}, function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#elder_select').append('<option value="' + response.data[i].elderId
				+ '">'+ response.data[i].bedNumber+'床'+' '+ response.data[i].roomName + ' '+ response.data[i].elderName
				+ ' ' +(response.data[i].houseTypeType==1?"VIP":"普通") +'</option>');
		}
		
		_addChangeListener("#elder_select", function() {
			var option = $('#elder_select option:selected');
			if(view && view.data) {
				view.data.elderId = $('#elder_select').val();
				view.data.elderName = option.text();
				
				if(load_elderFamilyMember_by_elderId) {
					load_elderFamilyMember_by_elderId(view.data.elderId);
				}
			}
		});
	});
}

function findElderByBedId() {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();
	
	/*
	var elderBeHospitalId = $('#elderBeHospital_select').val();
	*/

	find_basic_by_json(path+'/elderBeHospital/findDetail', {id: elderBeHospital.beHospitalId}, function(response) {
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		if(response.data[0].beStatus>=1) {
			$('#ulOutDate').hide();
			$('#btnOutOrderOK').hide();
		}else {
			$('#ulOutDate').show();
			$('#btnOutOrderOK').show();
		}
		
		for(var i=0; i<response.data.length; i++) {
			elderBeHospital.buildingId = response.data[i].buildingId;
			elderBeHospital.buildingName = response.data[i].buildingName;
			elderBeHospital.floorId = response.data[i].floorId;
			elderBeHospital.floorName = response.data[i].floorName;
			elderBeHospital.roomNumber = response.data[i].roomNumber;
			elderBeHospital.roomType = response.data[i].roomType;
			elderBeHospital.bedNumber = response.data[i].bedNumber;
			elderBeHospital.isDetail = 'yes';
			
			if(view && view.data) {
				if(response.data[i].elderForm) {
					view.data.elderId = response.data[i].elderForm.elderId;
					view.data.elderName = response.data[i].elderForm.name;
				}
				
				if(response.data[i].userVipCardForm) {
					view.data.remainMonths = getRemainMonths(response.data[i].userVipCardForm);
					view.data.remainNumber = getRemainNumber(response.data[i].userVipCardForm);
				}
				
				if(response.data[i].elderWalletForm) {
					view.data.walletBalance = response.data[i].elderWalletForm.walletBalance;
				}
			}
		}
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
				getSubsidizationValue:function(subsidization, subsidizationValue) {
					return getSubsidizationValue(subsidization, subsidizationValue);
				},
				getSubsidizationUnit:function(subsidization) {
					return getSubsidizationUnit(subsidization);
				},
				getSubsidizationText:function(subsidization, subsidizationValue) {
					return getSubsidizationText(subsidization, subsidizationValue);
				},
				getConsDeductionText:function(consumptionWay, consDeductionWay) {
					return getConsDeductionText(consumptionWay, consDeductionWay);
				},
				//总共可扣月数
				getTotalMonths:function(todo) {
					return getTotalMonths(todo);
				},
				//剩余可扣月数
				getRemainMonths:function(todo) {
					return getRemainMonths(todo);
				},
				//已扣月数
				getAlreadyUsedMonths:function(todo) {
					return getAlreadyUsedMonths(todo);
				},
				//总共可扣天数
				getTotalNumber:function(todo) {
					return getTotalNumber(todo);
				},
				//剩余可扣天数
				getRemainNumber:function(todo) {
					return getRemainNumber(todo);
				},
				//已扣天数
				getAlreadyUsedNumber:function(todo) {
					return getAlreadyUsedNumber(todo);
				}
			}
		});
		
		$('#vue_vm_list div').show();
	});
}

function deleteBatch() {
	deleteBatch_basic(path+'/elderBeHospital/deleteBatch');
}
